function reset(json, $number, $company, $username, $userphone, $status0, $status1) {
    $number.val(json.number);
    $company.val(json.company);
    $username.val(json.username);
    $userphone.val(json.userphone);
    if (json.status === '已取件') {
        $status1.prop('checked', true);
    } else {
        $status0.prop('checked', true);
    }
}

$(function () {
    const $number = $("input:eq(0)");
    const $number1 = $("input:eq(1)");
    const $company = $("select");
    const $username = $("input:eq(2)");
    const $userphone = $("input:eq(3)");
    const $status0 = $("input[name='status'][value='0']");
    const $status1 = $("input[name='status'][value='1']");
    const $labelPhone = $("#phone");
    const $labelNumber = $("#labelNumber");
    const $labelNumber1 = $("#labelNumber1");
    const $res = $("#res,#res-header");
    const $spanSend = $("#sendMessage");
    const $labelSend = $spanSend.parent();
    const inputSend = $labelSend.children()[0];

    $labelSend.hide();

    let json;

    // 这个方法描述了修改后是否显示 ‘修改成功发送短信给XXX’ 复选框的逻辑
    let showCheckBox = function () {
        let flag = json.status === '未取件' && $status0.prop('checked') && json.userphone !== $userphone.val();//  '未取件' 且 电话号码改变
        flag = (json.status === '已取件' && $status0.prop('checked')) || flag;// '已取件' 修改为 '未取件' 或 flag
        if (test_ipt(testPhone, $userphone, $labelPhone) && flag) {
            $spanSend.text(' 修改成功发送短信给 ' + $userphone.val());
            inputSend.checked = true;
            $labelSend.show();
        } else {
            inputSend.checked = false;
            $labelSend.hide();
        }
    }

    let updateExpress = function () {

        if ((json.number === $number1.val()) &&
            (json.company === $company.val()) &&
            (json.username === $username.val()) &&
            (json.userphone === $userphone.val()) &&
            (json.status === '已取件' ? $status1.prop('checked') : $status0.prop('checked'))) {
            layer.msg('还没有做任何修改呢');
            return false;
        }

        $number1.val($number1.val().trim());
        if (!test_ipt(testNumber, $number1, $labelNumber1)) {
            layer.msg('快递单号格式有误');
            return false;
        }
        let result = {
            id: json.id,
            number: $number1.val(),
            company: $company.val(),
            username: $username.val(),
            status: $status0.prop('checked') ? 0 : 1,
            sendMessage: inputSend.checked
        };
        if (json.userphone !== $userphone.val()) {
            if (!test_ipt(testPhone, $userphone, $labelPhone)) {
                layer.msg('请输入正确的手机号码');
                return false;
            }
            result.userphone = $userphone.val();
        }
        let windowId = layer.load();
        $.getJSON("/admin/views/express/update.do", result, function (data) {
            layer.close(windowId);
            layer.msg(data.message);
            if (data.status === 0) {
                flag = false;
                inputSend.checked = false;
                $labelSend.hide();
                $number.val("")
                $company.val("顺丰速递")
                $username.val("")
                $userphone.val("")
                $res.hide();
            }
        });
        return false;
    };


    let flag = false;
    let timeout;
    let findingNumber;
    let findByNumber = function () {
        $number.val($number.val().trim());
        if (!test_ipt(testNumber, $number, $labelNumber)) {
            layer.msg('快递单号格式有误');
            return false;
        }
        if (timeout !== undefined) clearTimeout(timeout);
        timeout = setTimeout(function () {
            flag = false;
        }, 10000);
        if (findingNumber === $number.val() && flag) {
            layer.msg('请勿重复提交同一内容');
            return false;
        }
        findingNumber = $number.val();
        let windowId = layer.load();
        $.getJSON("/admin/views/express/getByNumber.do", {
            number: $number.val()
        }, function (data) {
            layer.close(windowId);
            layer.msg(data.message);
            flag = true;
            if (data.status === 0) {
                json = data.data;
                reset(json, $number1, $company, $username, $userphone, $status0, $status1);
                $res.fadeIn(1000);
            }
        });
        return false;
    };

    let keyPressEvent = function (key) {
            if (key.key === 'Enter') {
                findByNumber();
                onkeypress = undefined;
            }
        };

    // 输入时的表单验证
    $number.bind('input propertychange', function () {
        onkeypress = keyPressEvent;
        test_ipt(testNumber, $number, $labelNumber)
    });
    $number1.bind('input propertychange', function () {
        test_ipt(testNumber, $number1, $labelNumber1);
    });
    $userphone.bind('input propertychange', showCheckBox);

    $status0.change(showCheckBox);
    $status1.change(showCheckBox);

    $("span:eq(1)").click(findByNumber);

    $("span:eq(4)").click(updateExpress);

    $("span:eq(5)").click(function () {
        reset(json, $number1, $company, $username, $userphone, $status0, $status1);
        showCheckBox();
    });
});